#!/usr/bin/env python
# -- coding=utf-8 --

import os

from tornado import web, options, httpserver, ioloop

options.define("port", 8000, type=int, help="端口号是必须的")


class IndexHandler(web.RequestHandler):
    def get(self):
        self.write("<h1>Index</h1>")


class GoRegister(web.RequestHandler):
    def get(self):
        print("get method")
        self.render("register_user.html")


class RegisterHandler(web.RequestHandler):
    def post(self):
        username = self.get_argument("username")
        password = self.get_argument("password")
        mydict = {"username": username, "password": password}
        # return self.render("register_success.html", username=str.upper(username), password=str.upper(password))
        return self.render("register_success.html", **mydict)


class BooksHandler(web.RequestHandler):
    def get(self):
        books = ["神雕侠侣", "倚天屠龙记", "西游记"]
        return self.render("books.html", books=books)


def main():
    options.parse_command_line()
    app = web.Application(handlers=[
        (r"^/$", IndexHandler),
        (r"^/register$", RegisterHandler),
        (r"^/goregister$", GoRegister),
        (r"^/books", BooksHandler),

    ],
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        debug=True
    )
    print(os.path.join(os.path.dirname(__file__), "templates"))
    http_server = httpserver.HTTPServer(app)
    print(options.options.port)
    http_server.listen(options.options.port)
    ioloop.IOLoop.current().start()


if __name__ == '__main__':
    main()
